package service

import (
	"github.com/gin-gonic/gin"
	"github.com/gogf/gf/v2/crypto/gmd5"
	"github.com/gogf/gf/v2/util/guid"
	"wx_build/core/jwt"
	"wx_build/core/msg"
	"wx_build/global"
	"wx_build/model/tables"
	"wx_build/utils"
)

type UserService struct {
	tables.User
	Page        int    `json:"page"`
	Size        int    `json:"size"`
	Search      string `json:"search"`
	NewPassword string `json:"new_password"`
}

// CreateUser 创建用户
func (c *UserService) CreateUser(g *gin.Context) {
	c.UserId = guid.S()
	query := tables.User{
		Username: c.Username,
	}
	err := query.QueryOne(&query)
	if err == nil {
		msg.Error(g, "该用户已经存在", nil)
		return
	}
	c.Password = gmd5.MustEncryptString(c.Password)
	err = c.User.Add(&c.User)
	if err != nil {
		msg.Error(g, "创建用户失败", err)
		return
	}
	msg.Success(g, "创建用户成功")
	return

}

// List 查询用户列表
func (c *UserService) List(g *gin.Context) {
	list := new([]tables.User)
	db := global.Sqlite
	if c.Search != "" {
		db = db.Where("nickname LIKE ?", "%"+c.Search+"%")
	}
	var count int64
	db.Find(&list).Count(&count)
	db.Order("created_at desc").Limit(c.Size).Offset((c.Page - 1) * c.Size).Find(&list)
	data := make([]map[string]interface{}, len(*list))
	for k, v := range *list {
		item, _ := utils.ToMap(v)
		item["create_time"] = v.CreatedAt.Format("2006-01-02 15:04:05")
		item["update_time"] = v.UpdatedAt.Format("2006-01-02 15:04:05")
		data[k] = item
	}
	res := make(map[string]interface{})
	res["count"] = count
	res["list"] = data
	msg.Success(g, "数据查询成功了", res)
	return

}

// Delete 删除用户
func (c *UserService) Delete(g *gin.Context) {
	query := tables.User{
		UserId: c.UserId,
	}
	err := query.QueryOne(&query)
	if err != nil {
		msg.Error(g, "用户不存在", err)
		return
	}
	db := global.Sqlite
	err = db.Delete(&query).Error
	if err != nil {
		msg.Error(g, "删除用户失败", err)
		return
	}
	msg.Success(g, "删除用户成功")
	return
}

// Login 用户登录
func (c *UserService) Login(g *gin.Context) {
	user := tables.User{
		Username: c.Username,
	}
	err := user.QueryOne(&user)
	if err != nil {
		msg.Error(g, "用户还未注册", err)
		return
	}
	if gmd5.MustEncryptString(c.Password) != user.Password {
		msg.Error(g, "您输入的密码不正确", nil)
		return
	}
	claims := new(jwt.UserClaims)
	claims.Username = user.Username
	claims.UserId = user.UserId
	claims.Nickname = user.Nickname
	token := jwt.GenerateToken(claims)
	res := make(map[string]interface{})
	userInfo, _ := utils.ToMap(user)
	res["user_info"] = userInfo
	res["token"] = token
	g.SetCookie("admin_token", token, 864000*7, "/", "*", false, true)
	msg.Success(g, "用户登录成功", res)
	return
}
func (c *UserService) SetPassword(g *gin.Context) {
	//user, _ := g.Get("admin_user")
	//thisUser := user.(tables.User)
	query := tables.User{
		UserId: c.UserId,
	}
	err := query.QueryOne(&query)
	if err != nil {
		msg.Error(g, "用户不存在", err)
		return
	}
	if gmd5.MustEncryptString(c.Password) != query.Password {
		msg.Error(g, "老密码错误", nil)
		return
	}
	query.Password = gmd5.MustEncryptString(c.NewPassword)
	db := global.Sqlite
	err = db.Save(&query).Error
	if err != nil {
		msg.Error(g, "修改用户密码失败", err)
		return
	}
	msg.Success(g, "修改密码成功")
	return

}
